package fhv.at.imageprocessing.roiImage;

import java.awt.Rectangle;
import java.io.Serializable;

import javax.media.jai.PlanarImage;

import fhv.at.imageprocessing.ImageChangedEvent;
import fhv.at.imageprocessing.ImageChangedListener;
import fhv.at.imageprocessing.ImageHelper;
import fhv.at.imageprocessing.ImageNeededEvent;
import fhv.at.imageprocessing.ImageProcessor;
import fhv.at.imageprocessing.editors.ROIEditorHelper;

/**
 * Region Of Interest image bean
 * 
 * @author Wetzel & Rusch
 * 
 */
public class ROIImage implements ImageChangedListener, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private final ROIEditorHelper roiEditorHelper;
	private final ImageHelper imageHelper;
	private PlanarImage planarImage;

	public ROIImage() {
		int maxParents = 1;
		imageHelper = new ImageHelper(maxParents, this);
		roiEditorHelper = new ROIEditorHelper();
		x1 = 0;
		y1 = 0;
		x2 = 50;
		y2 = 50;
	}

	public void setRoiEditorHelper(ROIEditorHelper roiEditorHelper) {
		this.x1 = roiEditorHelper.getX1();
		this.y1 = roiEditorHelper.getY1();
		this.x2 = roiEditorHelper.getX2() - this.x1;
		this.y2 = roiEditorHelper.getY2() - this.y1;
		if (allPropsSet()) {
			imageHelper.fireImageNeededEvent();
		}
	}

	public ROIEditorHelper getRoiEditorHelper() {
		roiEditorHelper.setX1(x1);
		roiEditorHelper.setY1(y1);
		roiEditorHelper.setX2(x2);
		roiEditorHelper.setY2(y2);
		return roiEditorHelper;
	}

	public int getX1() {
		return x1;
	}

	public void setX1(int x1) {
		this.x1 = x1;
		if (allPropsSet()) {
			imageHelper.fireImageNeededEvent();
		}
	}

	public int getY1() {
		return y1;
	}

	public void setY1(int y1) {
		this.y1 = y1;
		if (allPropsSet()) {
			imageHelper.fireImageNeededEvent();
		}
	}

	public int getX2() {
		return x2;
	}

	public void setX2(int x2) {
		this.x2 = x2;
		if (allPropsSet()) {
			imageHelper.fireImageNeededEvent();
		}
	}

	public int getY2() {
		return y2;
	}

	public void setY2(int y2) {
		this.y2 = y2;
		if (allPropsSet()) {
			imageHelper.fireImageNeededEvent();
		}
	}

	/* Eigentliche Bildbearbeitung */
	@Override
	public void manipulateImage() {
		if (planarImage != null) {
			ImageProcessor ip = ImageProcessor.getInstance();
			if (allPropsSet()) {
				planarImage = ip.getROI(planarImage, new Rectangle(x1, y1, x2,
						y2));
				imageHelper.fireImageChangedEvent();
			}
		}
	}

	private boolean allPropsSet() {
		if (x2 > 0 && y2 > 0) {
			return true;
		} else {
			return false;
		}
	}

	/* Implementation fur Eventregistrierung */
	@Override
	public void addImageChangedListener(ImageChangedListener icl) {
		imageHelper.addImageChangedListener(icl);
	}

	@Override
	public void removeImageChangedListener(ImageChangedListener icl) {
		imageHelper.removeImageChangedListener(icl);
	}

	@Override
	public void addImageNeededListener(ImageChangedListener icl) {
		imageHelper.addImageNeededListener(icl);
	}

	@Override
	public void removeImageNeededListener(ImageChangedListener icl) {
		imageHelper.removeImageNeededListener(icl);
	}

	/* Implementation fur ImageChangedListener */
	@Override
	public void imageValueChanged(ImageChangedEvent ice) {
		System.out.println(getClass().getSimpleName() + " imageValueChanged");
		planarImage = imageHelper.getPlanarImage(ice);
		roiEditorHelper.setPlanarImage(planarImage);
		manipulateImage();
	}

	@Override
	public void imageNeeded(ImageNeededEvent ice) {
		System.out.println(getClass().getSimpleName() + " imageNeeded");
		imageHelper.fireImageChangedEvent();
		// imageHelper.fireImageChangedEvent(ice);
	}

	@Override
	public ImageChangedListener getRefFromSource() {
		return this;
	}

	@Override
	public PlanarImage getPlanarImage() {
		return planarImage;
	}
}
